package org.pinda.gui;

import static org.johnnei.engine.render.RenderObject.VERTEX_COLOR;

import java.util.ArrayList;

import org.johnnei.engine.render.ColorHelper;
import org.johnnei.engine.render.TextRenderer;
import org.pinda.network.ServerBrowser;
import org.pinda.network.ServerInfo;

public class MultiplayerBrowserGui extends Gui {
	
	/**
	 * All servers received as reported by the game browser
	 */
	private ArrayList<ServerInfo> servers;
	private ServerBrowser serverBrowser;
	
	private GuiButton btnReturnToMenu;
	private GuiButton btnConnect;
	private GuiButton btnHost;
	
	public MultiplayerBrowserGui(Gui parent) {
		super(parent, VERTEX_COLOR);
		
		serverBrowser = new ServerBrowser(this);
		serverBrowser.start();
		servers = new ArrayList<>();
		
		btnReturnToMenu = new GuiButton(parent, 100, 980, 20, true, "Return to menu", new ColorHelper(0x33, 0x33, 0x33), null);
		btnConnect = new GuiButton(parent, 245, 980, 20, true, "Connect", new ColorHelper(0x33, 0x33, 0x33), null);
		btnHost = new GuiButton(parent, 330, 980, 20, true, "Create Room", new ColorHelper(0x33, 0x33, 0x33), null);
		
		addComponent(btnReturnToMenu);
		addComponent(btnConnect);
		addComponent(btnHost);
	}
	
	/**
	 * Adds a server to the browser
	 * @param server
	 */
	public void addServer(ServerInfo server) {
		servers.add(server);
	}
	
	@Override
	public void onTick(int deltaMs) {
		if(btnReturnToMenu.isClicked()) {
			switchGui(new MainMenuGui(getParent()));
		} else if(btnHost.isClicked()) {
			switchGui(new CreateRoomGui(getParent(), this), false);
		}
	}
	
	@Override
	public void render() {
		super.render();
		//Title
		TextRenderer.drawCentered(getCenterX(), 50, "Server Browser");
		//Header
		TextRenderer.draw(110, 75, "Room name   Players   Avg. Rating");
	}

}
